package com.coder520.mamabike.b.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.coder520.mamabike.b.R;
import com.coder520.mamabike.b.presenter.MainPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * Created by zhongyanli on 2017/10/13.
 */

public class MainActivity extends BaseActivity<MainPresenter> {
    @BindView(R.id.edit_phone_number)
    EditText editPhoneNumber;
    @BindView(R.id.btn_input_number)
    Button btnInputNumber;
    @BindView(R.id.text_bike_number)
    TextView textBikeNumber;
    @BindView(R.id.btn_lock)
    Button btnLock;
    @BindView(R.id.text_bike_state)
    TextView textBikeState;
    @BindView(R.id.btn_showQR)
    Button btnShowQR;


    public enum BikeState {
        FREE, IN_USE
    }

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        ButterKnife.bind(this);
    }

    @OnTextChanged(value = R.id.edit_phone_number, callback = OnTextChanged.Callback.TEXT_CHANGED)
    void onTextChanged(CharSequence s, int start, int before, int count) {
        boolean enableInputBtn = s.length() >= 6;
        btnInputNumber.setEnabled(enableInputBtn);
    }

    @OnClick(R.id.btn_input_number)
    void onInputNumberClick() {
        String newNumber = editPhoneNumber.getText().toString();
        mPresenter.setCurrentBikeNumber(newNumber);
    }

    @OnClick(R.id.btn_showQR)
    public void onShowQRClicked() {
        mPresenter.showQrDialog();
    }

    @OnClick(R.id.btn_lock)
    public void onLockClick() {
        mPresenter.onLockClick();
    }

    public void setShowQrButtonEnabled(final boolean enabled) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                btnShowQR.setEnabled(enabled);
            }
        });
    }

    public void setTextBikeState(final BikeState bikeState) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                int bikeStateRes = -1;
                int bikeStateColor = -1;
                if (bikeState == BikeState.FREE) {
                    bikeStateRes = R.string.bike_state_free;
                    bikeStateColor = android.R.color.holo_green_light;
                } else if (bikeState == BikeState.IN_USE) {
                    bikeStateRes = R.string.bike_state_in_use;
                    bikeStateColor = android.R.color.holo_red_light;
                }
                textBikeState.setText(bikeStateRes);
                textBikeState.setBackgroundColor(getResources().getColor(bikeStateColor));
            }
        });
    }

    public void setLockBtnEnabled(final boolean enabled) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (btnLock.isEnabled() != enabled) {
                    btnLock.setEnabled(enabled);
                }
            }
        });
    }

    public void setCurrentBikeNumber(final String number) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textBikeNumber.setText(number);
            }
        });
    }
}
